home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1250 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.1 KB  |  51 lines

  1. Path: nntp.hut.fi!nntp!hakola
  2. From: hakola@cadmail.hut.fi (Petri Hakola)
  3. Newsgroups: comp.lang.c
  4. Subject: Splitting String ?
  5. Date: 12 Jan 1996 13:11:28 GMT
  6. Organization: Capten Napalm`s Thermonuclear League of Liberty
  7. Distribution: world
  8. Message-ID: <HAKOLA.96Jan12151128@jung.hut.fi>
  9. Reply-To: Petri.Hakola@hut.fi
  10. NNTP-Posting-Host: jung.hut.fi
  11.  
  12.  
  13.     Have I missed something (again:) or why doesn't this code
  14.     work? I should split dos-a-like-filename and add new postfix
  15.     instead of old one (i.e. DATA.TXT --> DATA.UPD  It seems to
  16.     work correctly if the filename has an old postfix, but if
  17.     there isn't one start won't return what it should.
  18.  
  19.                             - P -
  20.  
  21.  
  22. ---Clip---
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26. char *newname(char *s) {
  27.  
  28.    char *dot;
  29.    char *start;
  30.    char end[5];
  31.  
  32.    strcpy(end,".UPD");
  33.    start = s;
  34.    dot = strchr(s,'.');
  35.    if( dot == NULL) {
  36.      start[strlen(start)] = '\0';
  37.      dot = end;
  38.    } else {
  39.    *dot = '\0';
  40.    dot++;
  41.    dot = end;
  42.    }
  43.    strcat(start, dot);
  44.    return start;
  45. }
  46.  
  47. main() {
  48.   printf("%s\n",newname("LONGNAME.TXT"));
  49.   printf("%s\n",newname("NOEND"));
  50. }
  51.